//
//  ViewController.m
//  10803省会和城市
//
//  Created by student on 16/2/28.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property(strong,nonatomic)NSMutableArray *govermentArray;//所有省会的数组
@property(strong,nonatomic)NSMutableDictionary *cityDict;
@property(strong,nonatomic)NSMutableDictionary *govermentDict;
//省会和城市组成的字典
@property(strong,nonatomic)NSArray *CitiesArray;//所有城市数组
@property(strong,nonatomic)NSMutableArray *cityArray;
@property(strong,nonatomic)NSArray *ListArray;//文件取出数组
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *path=[[NSBundle mainBundle]pathForResource:@"area1" ofType:@"plist"];
    self.ListArray =[NSArray arrayWithContentsOfFile:path] ;
     self.govermentArray = [NSMutableArray array];
    self.cityArray=[NSMutableArray array];
    for (int i =0; i<self.ListArray.count; i++) {
        self.govermentDict=self.ListArray[i];
        [self.govermentArray addObject:self.govermentDict[@"State"]] ;//第一列数组
    }
    self.govermentDict=self.ListArray[0];
    self.CitiesArray=self.govermentDict[@"Cities"];
    for (int i=0; i<self.CitiesArray.count; i++) {
        self.cityDict=self.CitiesArray[i];
        
        [self.cityArray addObject:self.cityDict[@"city"]];
    }
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component==0) {
        return self.govermentArray.count;
    }
    else
    {
        return self.cityArray.count;
    }
}
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component==0)
    {
        return self.govermentArray[row];
    }
    else
    {
        return self.cityArray[row];
    }
    
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component==0)
    {
        self.govermentDict=self.ListArray[row];
        self.CitiesArray=self.govermentDict[@"Cities"];
        [self.cityArray removeAllObjects];
        for (int j=0; j<self.CitiesArray.count; j++)
        {
            self.cityDict=self.CitiesArray[j];
            [self.cityArray addObject:self.cityDict[@"city"]];//第二列数组
        }
        [self.pickerView reloadComponent:1];
        // 4. 重新滑动到第一行
        [self.pickerView selectRow:0 inComponent:1 animated:YES];
            
    }

}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return 120;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 40;
    
}
@end
